package jsf.uss.factory;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

    private static final SessionFactory SESSION_FACTORY = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
	try {
	    Configuration configuration = new Configuration();
	    configuration.configure("hibernate.cfg.xml");
	    ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder()
		    .applySettings(configuration.getProperties());

	    return configuration.buildSessionFactory(serviceRegistryBuilder
		    .buildServiceRegistry());
	}
	catch (Throwable e) {
	    System.err.println("Initial SessionFactory creation failed: " + e);
	    throw new ExceptionInInitializerError(e);
	}
    }

    public static SessionFactory getSessionFactory() {
	return SESSION_FACTORY;
    }
}
